using ns0;
using ns4;
using System;
namespace ns1
{
	public sealed class MonochromeRectangleDetector
	{
		private const int int_0 = 32;
		private BitMatrix bitMatrix_0;
		public MonochromeRectangleDetector(BitMatrix image)
		{
			this.bitMatrix_0 = image;
		}
		public ResultPoint[] detect()
		{
			int height = this.bitMatrix_0.Height;
			int width = this.bitMatrix_0.Width;
			int num = height >> 1;
			int num2 = width >> 1;
			int num3 = Math.Max(1, height / 256);
			int num4 = Math.Max(1, width / 256);
			int int_ = height;
			int int_2 = width;
			ResultPoint resultPoint = this.method_0(num2, 0, 0, int_2, num, -num3, 0, int_, num2 >> 1);
			int int_3 = (int)resultPoint.Y - 1;
			ResultPoint resultPoint2 = this.method_0(num2, -num4, 0, int_2, num, 0, int_3, int_, num >> 1);
			int int_4 = (int)resultPoint2.X - 1;
			ResultPoint resultPoint3 = this.method_0(num2, num4, int_4, int_2, num, 0, int_3, int_, num >> 1);
			int_2 = (int)resultPoint3.X + 1;
			ResultPoint resultPoint4 = this.method_0(num2, 0, int_4, int_2, num, num3, int_3, int_, num2 >> 1);
			int_ = (int)resultPoint4.Y + 1;
			resultPoint = this.method_0(num2, 0, int_4, int_2, num, -num3, int_3, int_, num2 >> 2);
			return new ResultPoint[]
			{
				resultPoint,
				resultPoint2,
				resultPoint3,
				resultPoint4
			};
		}
		private ResultPoint method_0(int int_1, int int_2, int int_3, int int_4, int int_5, int int_6, int int_7, int int_8, int int_9)
		{
			int[] array = null;
			int num = int_5;
			int num2 = int_1;
			while (num < int_8 && num >= int_7 && num2 < int_4 && num2 >= int_3)
			{
				int[] array2;
				if (int_2 == 0)
				{
					array2 = this.method_1(num, int_9, int_3, int_4, true);
				}
				else
				{
					array2 = this.method_1(num2, int_9, int_7, int_8, false);
				}
				if (array2 != null)
				{
					array = array2;
					num += int_6;
					num2 += int_2;
				}
				else
				{
					if (array == null)
					{
						throw ReaderException.Instance;
					}
					ResultPoint result;
					if (int_2 == 0)
					{
						int num3 = num - int_6;
						if (array[0] < int_1)
						{
							if (array[1] > int_1)
							{
								result = new ResultPoint((float)((int_6 > 0) ? array[0] : array[1]), (float)num3);
							}
							else
							{
								result = new ResultPoint((float)array[0], (float)num3);
							}
						}
						else
						{
							result = new ResultPoint((float)array[1], (float)num3);
						}
					}
					else
					{
						int num4 = num2 - int_2;
						if (array[0] < int_5)
						{
							if (array[1] > int_5)
							{
								result = new ResultPoint((float)num4, (float)((int_2 < 0) ? array[0] : array[1]));
							}
							else
							{
								result = new ResultPoint((float)num4, (float)array[0]);
							}
						}
						else
						{
							result = new ResultPoint((float)num4, (float)array[1]);
						}
					}
					return result;
				}
			}
			throw ReaderException.Instance;
		}
		private int[] method_1(int int_1, int int_2, int int_3, int int_4, bool bool_0)
		{
			int num = int_3 + int_4 >> 1;
			int i = num;
			while (i >= int_3)
			{
				if (bool_0 ? this.bitMatrix_0.get_Renamed(i, int_1) : this.bitMatrix_0.get_Renamed(int_1, i))
				{
					i--;
				}
				else
				{
					int num2 = i;
					do
					{
						i--;
					}
					while (i >= int_3 && !(bool_0 ? this.bitMatrix_0.get_Renamed(i, int_1) : this.bitMatrix_0.get_Renamed(int_1, i)));
					int num3 = num2 - i;
					if (i < int_3 || num3 > int_2)
					{
						i = num2;
						IL_8F:
						i++;
						int j = num;
						while (j < int_4)
						{
							if (bool_0 ? this.bitMatrix_0.get_Renamed(j, int_1) : this.bitMatrix_0.get_Renamed(int_1, j))
							{
								j++;
							}
							else
							{
								num2 = j;
								do
								{
									j++;
								}
								while (j < int_4 && !(bool_0 ? this.bitMatrix_0.get_Renamed(j, int_1) : this.bitMatrix_0.get_Renamed(int_1, j)));
								num3 = j - num2;
								if (j >= int_4 || num3 > int_2)
								{
									j = num2;
									IL_130:
									j--;
									return (j > i) ? new int[]
									{
										i,
										j
									} : null;
								}
							}
						}
						goto IL_130;
					}
				}
			}
			goto IL_8F;
		}
	}
}
